package A2;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class A3 {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
            try (Socket socket = new Socket("127.0.0.1",8080);
                 DataInputStream in = new DataInputStream(socket.getInputStream());
                 DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                 BufferedReader key =new BufferedReader(new InputStreamReader(System.in));
            ){
                while (true){
                    String key1 = key.readLine();
                    if (key1.equalsIgnoreCase("y")){
                        break;
                    }
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("name",key1);
                    out.writeUTF(jsonObject.toString());
                    out.flush();
                    String name = in.readUTF();
                    jsonObject = new JSONObject(name);
                    System.out.println(jsonObject);
                }
            }catch (Throwable t1 ){
                t1.printStackTrace();
            }
        });
        t.start();
    }
}
